%% 创建储能型SVG控制系统（跟网/构网双模式）
function create_control_system()
    % 初始化参数
    SVG_Star_10kV_System_Init;
    
    % 创建控制系统模型
    model_name = 'SVG_Control_System';
    if bdIsLoaded(model_name)
        close_system(model_name, 0);
    end
    new_system(model_name);
    open_system(model_name);
    
    %% 1. 创建电网强度检测模块
    create_grid_strength_detection(model_name);
    
    %% 2. 创建跟网/构网模式切换模块
    create_mode_switching(model_name);
    
    %% 3. 创建PLL锁相环（跟网模式）
    create_pll_system(model_name);
    
    %% 4. 创建虚拟同步机控制（构网模式）
    create_vsm_control(model_name);
    
    %% 5. 创建功率控制环
    create_power_control(model_name);
    
    %% 6. 创建电流控制环
    create_current_control(model_name);
    
    %% 7. 创建调制策略
    create_modulation_strategy(model_name);
    
    %% 8. 创建电压均衡控制
    create_voltage_balancing(model_name);
    
    %% 9. 创建频率支撑控制
    create_frequency_support(model_name);
    
    %% 10. 创建保护系统
    create_protection_system(model_name);
    
    % 保存模型
    save_system(model_name);
    disp(['控制系统模型 ' model_name ' 创建完成！']);
end

%% 子函数：创建电网强度检测模块
function create_grid_strength_detection(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Grid_Strength_Detection']);
    set_param([model_name '/Grid_Strength_Detection'], 'Position', [100 100 300 200]);
    
    subsys = [model_name '/Grid_Strength_Detection'];
    
    % 输入端口
    add_block('built-in/Inport', [subsys '/Grid_V']);
    add_block('built-in/Inport', [subsys '/Grid_I']);
    
    % SCR计算
    add_block('built-in/Product', [subsys '/Impedance_Calc']);
    add_block('built-in/Gain', [subsys '/SCR_Calc']);
    
    % 强弱电网判断
    add_block('built-in/RelationalOperator', [subsys '/Grid_Type']);
    add_block('built-in/Constant', [subsys '/SCR_Threshold']);
    set_param([subsys '/SCR_Threshold'], 'Value', '3');
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/SCR_Value']);
    add_block('built-in/Outport', [subsys '/Is_Weak_Grid']);
end

%% 子函数：创建跟网/构网模式切换模块
function create_mode_switching(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Mode_Switching']);
    set_param([model_name '/Mode_Switching'], 'Position', [400 100 600 200]);
    
    subsys = [model_name '/Mode_Switching'];
    
    % 输入端口
    add_block('built-in/Inport', [subsys '/Is_Weak_Grid']);
    add_block('built-in/Inport', [subsys '/Manual_Override']);
    
    % 切换逻辑
    add_block('built-in/Switch', [subsys '/Mode_Select']);
    add_block('built-in/RateLimiter', [subsys '/Smooth_Transition']);
    set_param([subsys '/Smooth_Transition'], 'RisingSlewLimit', '10');
    set_param([subsys '/Smooth_Transition'], 'FallingSlewLimit', '-10');
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Control_Mode']);
end

%% 子函数：创建PLL锁相环
function create_pll_system(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/PLL_System']);
    set_param([model_name '/PLL_System'], 'Position', [100 300 300 400]);
    
    subsys = [model_name '/PLL_System'];
    
    % 三相电压输入
    add_block('built-in/Inport', [subsys '/Vabc']);
    
    % Park变换
    add_block('sps_lib/Control/abc to dq0', [subsys '/Park_Transform']);
    
    % PI控制器
    add_block('built-in/PID Controller', [subsys '/PLL_PI']);
    set_param([subsys '/PLL_PI'], 'Controller', 'PI');
    set_param([subsys '/PLL_PI'], 'P', '180');
    set_param([subsys '/PLL_PI'], 'I', '3200');
    
    % 积分器
    add_block('built-in/Integrator', [subsys '/Theta_Gen']);
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Theta']);
    add_block('built-in/Outport', [subsys '/Frequency']);
end

%% 子函数：创建虚拟同步机控制
function create_vsm_control(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/VSM_Control']);
    set_param([model_name '/VSM_Control'], 'Position', [400 300 600 400]);
    
    subsys = [model_name '/VSM_Control'];
    
    % 输入端口
    add_block('built-in/Inport', [subsys '/P_ref']);
    add_block('built-in/Inport', [subsys '/Q_ref']);
    add_block('built-in/Inport', [subsys '/P_meas']);
    add_block('built-in/Inport', [subsys '/Q_meas']);
    
    % 虚拟惯量控制
    add_block('built-in/TransferFcn', [subsys '/Virtual_Inertia']);
    set_param([subsys '/Virtual_Inertia'], 'Numerator', '[1]');
    set_param([subsys '/Virtual_Inertia'], 'Denominator', '[2 0.2 1]');
    
    % 虚拟阻尼控制
    add_block('built-in/Gain', [subsys '/Virtual_Damping']);
    set_param([subsys '/Virtual_Damping'], 'Gain', '10');
    
    % 电压幅值控制
    add_block('built-in/PID Controller', [subsys '/V_Controller']);
    set_param([subsys '/V_Controller'], 'Controller', 'PI');
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/V_ref']);
    add_block('built-in/Outport', [subsys '/Theta_ref']);
end

%% 子函数：创建功率控制环
function create_power_control(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Power_Control']);
    set_param([model_name '/Power_Control'], 'Position', [100 500 300 600]);
    
    subsys = [model_name '/Power_Control'];
    
    % 输入端口
    add_block('built-in/Inport', [subsys '/P_ref']);
    add_block('built-in/Inport', [subsys '/Q_ref']);
    add_block('built-in/Inport', [subsys '/P_meas']);
    add_block('built-in/Inport', [subsys '/Q_meas']);
    add_block('built-in/Inport', [subsys '/Control_Mode']);
    
    % 有功功率控制
    add_block('built-in/PID Controller', [subsys '/P_Controller']);
    set_param([subsys '/P_Controller'], 'Controller', 'PI');
    set_param([subsys '/P_Controller'], 'P', '0.5');
    set_param([subsys '/P_Controller'], 'I', '50');
    
    % 无功功率控制
    add_block('built-in/PID Controller', [subsys '/Q_Controller']);
    set_param([subsys '/Q_Controller'], 'Controller', 'PI');
    set_param([subsys '/Q_Controller'], 'P', '0.5');
    set_param([subsys '/Q_Controller'], 'I', '50');
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Id_ref']);
    add_block('built-in/Outport', [subsys '/Iq_ref']);
end

%% 子函数：创建电流控制环
function create_current_control(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Current_Control']);
    set_param([model_name '/Current_Control'], 'Position', [400 500 600 600]);
    
    subsys = [model_name '/Current_Control'];
    
    % 输入端口
    add_block('built-in/Inport', [subsys '/Id_ref']);
    add_block('built-in/Inport', [subsys '/Iq_ref']);
    add_block('built-in/Inport', [subsys '/Id_meas']);
    add_block('built-in/Inport', [subsys '/Iq_meas']);
    
    % d轴电流控制
    add_block('built-in/PID Controller', [subsys '/Id_Controller']);
    set_param([subsys '/Id_Controller'], 'Controller', 'PI');
    set_param([subsys '/Id_Controller'], 'P', '0.5');
    set_param([subsys '/Id_Controller'], 'I', '50');
    
    % q轴电流控制
    add_block('built-in/PID Controller', [subsys '/Iq_Controller']);
    set_param([subsys '/Iq_Controller'], 'Controller', 'PI');
    set_param([subsys '/Iq_Controller'], 'P', '0.5');
    set_param([subsys '/Iq_Controller'], 'I', '50');
    
    % 前馈解耦
    add_block('built-in/Gain', [subsys '/Decoupling_d']);
    add_block('built-in/Gain', [subsys '/Decoupling_q']);
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Vd_ref']);
    add_block('built-in/Outport', [subsys '/Vq_ref']);
end

%% 子函数：创建调制策略
function create_modulation_strategy(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Modulation_Strategy']);
    set_param([model_name '/Modulation_Strategy'], 'Position', [100 700 300 800]);
    
    subsys = [model_name '/Modulation_Strategy'];
    
    % 输入端口
    add_block('built-in/Inport', [subsys '/Vd_ref']);
    add_block('built-in/Inport', [subsys '/Vq_ref']);
    add_block('built-in/Inport', [subsys '/Theta']);
    
    % 反Park变换
    add_block('sps_lib/Control/dq0 to abc', [subsys '/Inverse_Park']);
    
    % 载波相移PWM
    add_block('built-in/Subsystem', [subsys '/CPS_PWM']);
    
    % 输出端口（每相11个H桥的开关信号）
    for i = 1:3
        for j = 1:11
            add_block('built-in/Outport', [subsys '/S_' char(64+i) num2str(j)]);
        end
    end
end

%% 子函数：创建电压均衡控制
function create_voltage_balancing(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Voltage_Balancing']);
    set_param([model_name '/Voltage_Balancing'], 'Position', [400 700 600 800]);
    
    subsys = [model_name '/Voltage_Balancing'];
    
    % 输入端口（所有H桥的直流电压）
    for i = 1:3
        for j = 1:11
            add_block('built-in/Inport', [subsys '/Vdc_' char(64+i) num2str(j)]);
        end
    end
    
    % 三级均衡控制
    % 1. 总体直流电压控制
    add_block('built-in/Subsystem', [subsys '/Overall_DC_Control']);
    
    % 2. 相间均衡控制
    add_block('built-in/Subsystem', [subsys '/Inter_Phase_Balance']);
    
    % 3. 相内均衡控制
    add_block('built-in/Subsystem', [subsys '/Intra_Phase_Balance']);
    
    % PI控制器
    add_block('built-in/PID Controller', [subsys '/DC_PI']);
    set_param([subsys '/DC_PI'], 'Controller', 'PI');
    set_param([subsys '/DC_PI'], 'P', '0.1');
    set_param([subsys '/DC_PI'], 'I', '10');
    
    % 输出端口（电压参考修正值）
    for i = 1:3
        for j = 1:11
            add_block('built-in/Outport', [subsys '/Vdc_ref_' char(64+i) num2str(j)]);
        end
    end
end

%% 子函数：创建频率支撑控制
function create_frequency_support(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Frequency_Support']);
    set_param([model_name '/Frequency_Support'], 'Position', [100 900 300 1000]);
    
    subsys = [model_name '/Frequency_Support'];
    
    % 输入端口
    add_block('built-in/Inport', [subsys '/f_meas']);
    add_block('built-in/Inport', [subsys '/f_ref']);
    
    % 频率偏差检测
    add_block('built-in/Sum', [subsys '/f_error']);
    add_block('built-in/Abs', [subsys '/f_error_abs']);
    
    % 死区控制（±0.1Hz）
    add_block('built-in/DeadZone', [subsys '/Deadzone']);
    set_param([subsys '/Deadzone'], 'LowerValue', '-0.1');
    set_param([subsys '/Deadzone'], 'UpperValue', '0.1');
    
    % 下垂控制
    add_block('built-in/Gain', [subsys '/Droop_Gain']);
    set_param([subsys '/Droop_Gain'], 'Gain', '1e6');  % 1MW/0.1Hz
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/P_support']);
end

%% 子函数：创建保护系统
function create_protection_system(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Protection_System']);
    set_param([model_name '/Protection_System'], 'Position', [400 900 600 1000]);
    
    subsys = [model_name '/Protection_System'];
    
    % 过流保护
    add_block('built-in/Subsystem', [subsys '/Overcurrent_Protection']);
    
    % 过压/欠压保护
    add_block('built-in/Subsystem', [subsys '/OV_UV_Protection']);
    
    % SOC保护
    add_block('built-in/Subsystem', [subsys '/SOC_Protection']);
    
    % 温度保护
    add_block('built-in/Subsystem', [subsys '/Temperature_Protection']);
    
    % 故障处理逻辑
    add_block('built-in/Logic', [subsys '/Fault_Logic']);
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Trip_Signal']);
    add_block('built-in/Outport', [subsys '/Alarm_Signal']);
end